package org.ipp4j.core.test;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.SequenceInputStream;
import java.util.Collections;

import org.apache.commons.io.FileUtils;
import org.ipp4j.core.Attribute;
import org.ipp4j.core.IppRequest;
import org.ipp4j.core.IppTools;
import org.ipp4j.core.IppUtils;
import org.ipp4j.core.standard.IppConstants;
import org.ipp4j.core.standard.Tag;
import org.junit.Test;

public class ExtractionReplacementTrial {

	private static final String PRINT_QUEUE_FILE = "src/test/samples/a4.queue";
	
	@Test
	public void test() throws IOException {
		InputStream ippStream = new FileInputStream(new File(PRINT_QUEUE_FILE));
		
		IppRequest request = IppUtils.decodeAsRequest(ippStream);
		request.updateAttribute(new Attribute(Tag.URI,
				IppConstants.NAME_PRINTER_URI,
				"ipp://172.19.24.124:631/printer"));
		FileUtils.copyInputStreamToFile(IppUtils.createStream(request), new File("c:\\ipputils1.txt"));
		
		ippStream = new FileInputStream(new File(PRINT_QUEUE_FILE));
		
		byte[] requestHeader = IppTools.extractIppHeader(ippStream,
				Collections.singletonMap(IppConstants.NAME_PRINTER_URI,
						"ipp://172.19.24.124:631/printer"));

		SequenceInputStream requestStream = new SequenceInputStream(
				new ByteArrayInputStream(requestHeader), ippStream);
		FileUtils.copyInputStreamToFile(requestStream, new File("c:\\ipputils2.txt"));
	}

}
